﻿function RootController($scope, service, $routeParams, $location, $http) {

    $scope.treeMenu = false;
    $scope._update = function (scope) {
        if (!scope)
            scope = $scope;
        if (!scope.$$phase || scope.$$phase == '')
            scope.$apply();
    };

    $scope.selectLanguage = function (lang) {
        $scope.texts = window['texts_' + lang];
        $scope._update();
    };
//    var currentPath = $location.path().toLowerCase();
//    if ((currentPath == '/forgotpassword')
//        || (currentPath == '/register')
//        || (currentPath == '/registercompleteinformation')
//        || (currentPath == '/registerconfirm')
//        || (currentPath == '/registersuccess')
//        || (currentPath == '/resetpassword'))
//    {
//        $scope.treeMenu = false;
//    }
    $scope.checkLogin = function (nextPage) {

        var currentPath = $location.path().toLowerCase();
        //Should use base64 encoding to encode the key here
        var loggedUser = sessionStorage.getItem('loggedUser');
        if ((!loggedUser || loggedUser == '') && ((currentPath != '/forgotpassword')
            && (currentPath != '/register')
            && (currentPath != '/registercompleteinformation')
            && (currentPath != '/registerconfirm')
            && (currentPath != '/registersuccess')
            && (currentPath != '/resetpassword'))) {
            $location.path('/Login');
        } else {
            $scope.loggedUser = loggedUser;
            var sInfoUser = sessionStorage.getItem('infoUser');
            service.infoUser = angular.fromJson(sInfoUser);
            $scope.treeMenu = true;
            //$scope._update($scope);
            if (nextPage)
                $location.path('/' + nextPage);
        }
        $scope._update($scope);
    };

    $scope.logOut = function () {
        $scope.loggedUser = null;
        sessionStorage.removeItem('loggedUser');
        $location.path('/Login');
        $scope.treeMenu = false;
    };

    $scope.selectLanguage('en');
    $scope.message = "Welcome to SmaTime project";
    $location.path('/Top');
    $scope.checkLogin('Top');

    $scope.$on('$viewContentLoaded', function () {
        $scope.checkLogin();
    });
}

